package trlygjj.tanruiliyigenjinjin.web.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import trlygjj.tanruiliyigenjinjin.domain.Curriculum;
import trlygjj.tanruiliyigenjinjin.page.PageResult;
import trlygjj.tanruiliyigenjinjin.query.CurriculumQueryObject;
import trlygjj.tanruiliyigenjinjin.service.ICurriculumService;
import trlygjj.tanruiliyigenjinjin.util.JsonResult;
import trlygjj.tanruiliyigenjinjin.util.PermissionName;

import java.util.List;

/**
 * Created by user on 2018/7/11.
 */
@Controller
@RequestMapping("curriculum")
public class CurriculumController {

    @Autowired
    private ICurriculumService curriculumService;

    /**
     * 提供一个默认不输入后缀,直接可以跳转到具有分页的列表页面
     * @return
     */
    @RequiresPermissions("curriculum:index")
    @PermissionName("课程页面")
    @RequestMapping("")
    public String index(){
        return "curriculum/list";
    }

    /**
     * 具有分页和高级查询
     * @return
     */
    @RequiresPermissions("curriculum:list")
    @PermissionName("课程数据")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(CurriculumQueryObject qo){
        PageResult result = curriculumService.query(qo);
        return result;
    }

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("listAll")
    @ResponseBody
    public List<Curriculum> listAll(){
        return curriculumService.selectAll();
    }

    /**
     * 删除
     * @param curriculumId 要删除的员工对象id
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("curriculum:delete")
    @PermissionName("课程删除")
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long curriculumId){
        JsonResult result = new JsonResult();
        try{
            curriculumService.deleteByPrimaryKey(curriculumId);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     *更新
     * @param curriculum 要更新的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("curriculum:update")
    @PermissionName("课程编辑")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Curriculum curriculum){
        JsonResult result = new JsonResult();
        try{
            curriculumService.updateByPrimaryKey(curriculum);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 保存
     * @param curriculum 要保存的员工对象
     * @return 返回JsonResult对象判断是否操作成功
     */
    @RequiresPermissions("curriculum:save")
    @PermissionName("课程保存")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Curriculum curriculum){
        JsonResult result = new JsonResult();
        try{
            curriculumService.insert(curriculum);
        }catch (Exception e){
            e.printStackTrace();
            result.mark("操作失败!");
        }
        return result;
    }

    /**
     * 查询课程名称
     * @param curriculumSeriseId
     * @return
     */
    @RequestMapping("selectCurriculumName")
    @ResponseBody
    public List<Curriculum> selectCurriculumName(Long curriculumSeriseId){
        return curriculumService.selectCurriculumNameBycurriculumSeriseId(curriculumSeriseId);
    }
}
